﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MaxLeaf.DAL;
using System.IO;

namespace MaxLeaf.WindowsOvernight
{
    public partial class frmUpdateOnHand : Form
    {

        string mstrPath = Application.StartupPath;
        string mstrFileName = "";

        public frmUpdateOnHand()
        {
            InitializeComponent();
            mstrFileName =mstrPath + "\\" + "OverNightForUpdateOnHand.xml";
        }

        private void btnRun_Click(object sender, EventArgs e)
        {
           // MaxLeaf.CallWebService.Program.Main( new string[]{ "999" }); 
            
            if (UpdateData())
            {
                this.Hide();
                RunOvernight();
            }
        }

        private bool RunOvernight()
        {
            string strFileName = "";
            if (!File.Exists(mstrFileName))
            {
                return false;
            }
            DataTable dtXML = new DataTable();
            dtXML.ReadXml(mstrFileName);
            DataRow dr = dtXML.Rows[0];           
            string strRunTime = dr["StartTime"].ToString().TrimEnd();
            if (!(DateTime.Parse(strRunTime).Hour == DateTime.Now.Hour && DateTime.Parse(strRunTime).Minute == DateTime.Now.Minute)) return false;

            UpdateOnHandOverNightDAL imDAL = new UpdateOnHandOverNightDAL();
            if (imDAL.UpdateOnHand())
            {
                return true;
            }
            else
            {
                return false;
            }            
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("如果退出此程序，系统将无法更新网站端库存数据。" + "\r\n" + "您确定是否继续?", "Info", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == System.Windows.Forms.DialogResult.Yes)
            {
                this.Close();
            }           
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (UpdateData())
            {
                this.Hide();
            }
        }

        private void LoadConfigTxt()
        {            
            string strFileName = "";            
            strFileName = mstrFileName;
            if (!File.Exists(strFileName))
            {
                txtRunTime.Text = "23:00";
                return;
            }
            DataTable dtTable = new DataTable();
            dtTable.ReadXml(strFileName);
            foreach (DataRow drRow in dtTable.Rows)
            {
                txtRunTime.Text = drRow["StartTime"].ToString();              
            }
        }

        private bool UpdateData()
        {
            string strConDB = string.Empty;

            DataTable dtXML = new DataTable("OverNightForUpdateOnHand");
            dtXML.Columns.Add(new DataColumn("StartTime", typeof(string)));            
            dtXML.Rows.Add();
            DataRow dr = dtXML.Rows[0];
            dr["StartTime"] = txtRunTime.Text;
            dtXML.AcceptChanges();

            if (File.Exists(mstrFileName)) File.Delete(mstrFileName);
            dtXML.WriteXml(mstrFileName, XmlWriteMode.WriteSchema, false);

            return true;           
        }

        private void notifyIcon1_Click(object sender, EventArgs e)
        {
            this.Show();
        }

        private void frmUpdateOnHand_Load(object sender, EventArgs e)
        {
            LoadConfigTxt();
        }
    }
}
